#include <iostream>
#include <cstring>
#include <string>
using namespace std;

#define DEEP_CPY

namespace DAY11_SHALLOW_CPY
{
    class Employee {
        public:
            char* name;
            int age;
            char* position;
            string objName;
        public:
            Employee(const char* n, int a, const char* p,const string str) : name(new char[strlen(n) + 1]), age(a), position(new char[strlen(p) + 1]) ,objName(str){
                strcpy(name, n);
                strcpy(position, p);
                cout << "构造函数 初始化参数" << endl;
            }

            // 浅拷贝构造函数
            Employee(const Employee& other) : name(other.name), age(other.age), position(other.position) ,objName(other.objName) {cout << "构造函数 浅拷贝" << endl;} 
        
            ~Employee() {
                
                delete[] this->name;
                delete[] this->position;
                cout << this->objName << "析构函数" << endl;
                
                
            }
        
            void print() const 
            {
                cout << "name: " << this->name << " age:" << this->age << " position: " << this->position << endl;
            }
    };
    
};


namespace DAY11_DEEP_CPY
{
    class Employee {
     public:
        char* name;
        int age;
        char* position;
        string objName;
    public:
        Employee(const char* n, int a, const char* p,string objname){

            this->name = new char[strlen(n) + 1];
            this->position = new char[strlen(p) + 1];
            this->age = a;
            this->objName = objname;
            strcpy(name, n);
            strcpy(position, p);
            cout << "构造函数 初始化参数" << endl;
        }

        // 深拷贝构造函数
        Employee(const Employee& other) : age(other.age), name(new char[strlen(other.name) + 1]), position(new char[strlen(other.position) + 1]),objName(other.objName) {
            strcpy(name, other.name);
            strcpy(position, other.position);
            cout << "构造函数 深拷贝" << endl;
        }
    
        ~Employee() {
            
            delete[] this->name;
            delete[] this->position;
            cout << this->objName << "析构函数" << endl;
            
            
        }
    
        void print() const 
        {
            cout << "name: " << this->name << " age:" << this->age << " position: " << this->position << endl;
        }
    };
};



int main(int argc, char *argv[])
{

#ifndef DEEP_CPY
    cout << "\n \n \n";
    cout << "***************************************************浅拷贝解析***************************************************" << endl;
    cout << "\n \n \n";
    // 浅拷贝
    {
        using namespace DAY11_SHALLOW_CPY;
        char hu1[] = "Tom";
        char hu2[] = "ShangHai";
        int ku1 = 23;

        char hu11[] = "Hubery";
        char hu22[] = "BeiJing";
        int ku11 = 28;

        cout << "*******step1*******" << endl;
        Employee t1(hu1,ku1,hu2,"t1");
        cout << "*******step2*******" << endl;

        Employee t2 = t1;

        cout << "*******step3*******" << endl;

        strcpy(t2.name,hu11);
        strcpy(t2.position,hu22);
        t2.objName = "t2";
        t2.age = ku11;

        cout << "t1:********* " << endl;
        t1.print();

        cout << "t2:********* " << endl;
        t2.print();
        cout << "*******step4*******" << endl;
        
    }
    /* 结果分析：
        明显修改t2中的一些值，改变了t1中的部分值，最后释放空间时有重复释放的报错，因此是浅拷贝
        {
            *******step1*******
            构造函数 初始化参数
            *******step2*******
            构造函数 浅拷贝
            *******step3*******
            t1:********* 
            name: Hubery age:23 position: BeiJing
            t2:********* 
            name: Hubery age:28 position: BeiJing
            *******step4*******
            t2析构函数
            free(): double free detected in tcache 2
            Makefile:228: recipe for target 'test_day11' failed
            make: *** [test_day11] Aborted (core dumped)
        }
    */

#else
    cout << "\n \n \n";
    cout << "***************************************************深拷贝解析***************************************************" << endl;
    cout << "\n \n \n";
   // 深拷贝
   {
        using namespace DAY11_DEEP_CPY;
        char hu1[] = "Tom";
        char hu2[] = "ShangHai";
        int ku1 = 23;

        char hu11[] = "Hubery";
        char hu22[] = "BeiJing";
        int ku11 = 28;

        cout << "*******step1*******" << endl;
        Employee t1(hu1,ku1,hu2,"t1");
        cout << "*******step2*******" << endl;

        Employee t2 = t1;

        cout << "*******step3*******" << endl;

        strcpy(t2.name,hu11);
        strcpy(t2.position,hu22);
        t2.objName = "t2";
        t2.age = ku11;

        cout << "t1:********* " << endl;
        t1.print();

        cout << "t2:********* " << endl;
        t2.print();
        cout << "*******step4*******" << endl;
        
    }

    /* 结果分析：
        明显修改t2的值，t1中的值都没有改变，且程序运行结束后，调用了各自的析构函数，因此是深拷贝
        {
            *******step1*******
            构造函数 初始化参数
            *******step2*******
            构造函数 深拷贝
            *******step3*******
            t1:********* 
            name: Tom age:23 position: ShangHai
            t2:********* 
            name: Hubery age:28 position: BeiJing
            *******step4*******
            t2析构函数
            t1析构函数
        }
    */
#endif

    return 0;
}